'use strict';
//app的设置
import React,{Component} from 'react';
import Header from '../common/header';
import styles from '../style/styles';
import Icon from 'react-native-vector-icons/Ionicons';
import Toast from '../common/ToastUtil';
import MyStroage from '../common/myStroage';
import {
    View,
    Text,
    TouchableOpacity,
    NativeModules,
    Image, DeviceEventEmitter
} from 'react-native';
//const HttpCache=NativeModules.CommonModule;
export default class appSetting extends Component{
    // 构造
      constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            cacheSize:19
        };
      }

    componentDidMount() {
        //HttpCache.getHttpCacheSize((rs)=>this.setState({cacheSize:rs}))
        // this.getHttpCache()
    }
    //关于我们
    about(){
        let that= this;
        that.props.navigation.push('AboutUs')
    }
    //清除缓存
    onHttpCache(){
        //HttpCache.clearCache((rs)=>this.setState({cacheSize:rs}));
        //if(this.state.cacheSize!='0K'){
        //    Toast.show('清除缓存成功');
        //}

    }


    //获取缓存
    getHttpCache(){
        console.log("====================getHttpCacheSize==js======================" );
        NativeModules.CommonModule.getHttpCacheSize().then((success=>{
            console.log("====================getHttpCacheSize======data:"+success);
            console.log("====getHttpCacheSize==js=" + success);
            let cacheToM = parseInt(success)/1024/1024;
            cacheToM = Math.floor(cacheToM*100)/100
            this.setState({
                cacheSize:parseFloat(cacheToM)
            })
        }));
    }

    //清楚缓存
    clearHttpCache(){
        // NativeModules.CommonModule.clearCache().then((success=>{
        //     this.setState({
        //         cacheSize:0
        //     })
        //     Toast.show('清除缓存成功');
        // }));
        this.setState({
            cacheSize:0
        })
        Toast.show('清除缓存成功');
    }

    async Cancellation(){
        try {
            await MyStroage.removeStroage('userinfo');
            let params = this.props.route.params
            params.onLogout()
            DeviceEventEmitter.emit('logout')
            this.props.navigation.popToTop();
        } catch (err) {
            LOG(err);
        }
    }

    render(){
        return(
            <View style={{flex:1,}}>
                <Header navigation={this.props.navigation}  title={'设置'}/>
                <View style={{flex:1,backgroundColor:"#f0f0f0"}}>
                    <View style={styles.user_info_setting}>
                        <TouchableOpacity style={styles.setting_list} onPress={()=>this.clearHttpCache()}>
                            <Text>清除本地缓存</Text>
                            <View style={styles.setting_list_r}>
                                <Text style={styles.setting_txt}>{this.state.cacheSize} M</Text>
                                <Image
                                    source={require('../imgs/right_arrow.png')}
                                    resizeMode={'cover'}
                                    style={{width: 15, height: 15,}}
                                />
                            </View>
                        </TouchableOpacity>
                        <TouchableOpacity style={styles.setting_list} onPress={()=>this.about()}>
                            <Text>关于我们</Text>
                            <View style={styles.setting_list_r}>
                                <Image
                                    source={require('../imgs/right_arrow.png')}
                                    resizeMode={'cover'}
                                    style={{width: 15, height: 15,}}
                                />
                            </View>
                        </TouchableOpacity>
                    </View>
                    <View style={styles.cannel_wrap}>
                        <TouchableOpacity style={styles.cannel_btn} onPress={()=>this.Cancellation()}>
                            <Text style={styles.cannel_btn_txt}>注销</Text>
                        </TouchableOpacity>
                    </View>
                </View>
            </View>
        )
    }
}